import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/foundation/change_notifier.dart';

class EditMessageSelectionControls extends MaterialTextSelectionControls {

  static const double _kToolbarContentDistanceBelow = 20.0;
  static const double _kToolbarContentDistance = 8.0;

  @override
  Widget buildToolbar(BuildContext context, Rect globalEditableRegion, double textLineHeight, Offset selectionMidpoint, List<TextSelectionPoint> endpoints, TextSelectionDelegate delegate, ValueListenable<ClipboardStatus>? clipboardStatus, Offset? lastSecondaryTapDownPosition) {
    final TextSelectionPoint startTextSelectionPoint = endpoints[0];
    final TextSelectionPoint endTextSelectionPoint = endpoints.length > 1 ? endpoints[1] : endpoints[0];
    final Offset anchorAbove = Offset(
      globalEditableRegion.left + selectionMidpoint.dx,
      globalEditableRegion.top +
          startTextSelectionPoint.point.dy -
          textLineHeight -
          _kToolbarContentDistance,
    );
    final Offset anchorBelow = Offset(
      globalEditableRegion.left + selectionMidpoint.dx,
      globalEditableRegion.top +
          endTextSelectionPoint.point.dy +
          _kToolbarContentDistanceBelow,
    );

    return TextSelectionToolbar(
      anchorAbove: anchorAbove,
      anchorBelow: anchorBelow,
      toolbarBuilder: (context, _) => Material(
        child: SizedBox(
          width: 230,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Material(child: ListTile(title: Text('复制'), onTap: (){
                  delegate.selectAll(SelectionChangedCause.toolbar);
                  delegate.copySelection(SelectionChangedCause.toolbar);
                },)
              ),
              Material(child: ListTile(title: Text('全选'), onTap: (){
                  delegate.selectAll(SelectionChangedCause.toolbar);
                },
              )),
              Material(child: ListTile(title: Text('粘贴'), onTap: (){
                  delegate.pasteText(SelectionChangedCause.toolbar);
                },)
              ),
            ],
          ),
        ),
      ),
      children: const [SizedBox.shrink()],
    );
  }

}